package com.sysman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sysman.HttpTools.DataAnalyze;
import com.sysman.HttpTools.DataAnalyzeDetails;
import com.sysman.HttpTools.HttpRequest;
import com.sysman.po.HospinfoCustom;
import com.sysman.po.Hosplogininfo;
import com.sysman.service.HospitalService;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年8月11日 下午10:43:51 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/Hospital")
public class HospitalController {
	@Autowired
	private HospitalService hospitalService;
	/**
	 * 这里是医院端列表初始化界面，实现对所有医院端注册用户（包括正式用户和非正式用户）的管理
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/hospitallist")
	public ModelAndView hospitallist()throws Exception {
		//List<HosplogininfoCustom> hosplogininfoList = hospitalService.InitHosplogininfoList(null);
		ModelAndView model = new ModelAndView();
		//model.addObject("hosplogininfoList", hosplogininfoList);
		model.setViewName("View/HospitalMan/HospitalList");
		return model;
	}
	/**
	 * 医院端列表动态加载
	 * @param limit
	 * @param offset
	 * @param type
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/hospitalListAjax")
	public @ResponseBody Map<String,Object> hospitalListAjax(Integer limit,Integer offset,Integer type,Integer hospadrprovince,Integer hospadrcity,Integer hospadrarea,String hosplevel,String hosploginphone,String startdatetime,String enddatetime, HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		System.out.println("输出接收的参数是："+limit+"//"+offset+"//"+type+"//"+adminloginid);
		String HospitalParam = "limit="+limit+"&offset="+offset+"&type="+type+"&adminloginid="+adminloginid+"&hospadrprovince="+hospadrprovince+"&hospadrcity="+hospadrcity+"&hospadrarea="+hospadrarea+"&hosplevel="+hosplevel+"&hosploginphone="+hosploginphone+"&startdatetime="+startdatetime+"&enddatetime="+enddatetime;//参数串
		System.out.println("输出这个参数串："+HospitalParam);
		List<Map<String,Object>> jsonlist=new ArrayList<>();
		Integer total=0;
		Map<String, Object> map = new HashMap<String, Object>(); 
		String code="";
		String msg="";
		if(adminloginid==null){
	        msg="请先登录";
	        code="-100";
		}else {
			try {
				JSONObject HospitalListjson = HttpRequest.sendPost("listhospital", HospitalParam);
				System.out.println("输出接收到的json数据是："+HospitalListjson);
				
				msg = HospitalListjson.get("msg").toString();
				code =HospitalListjson.get("code").toString();
				
				try {
					JSONObject jsonobjectdata =HospitalListjson.getJSONObject("data");
					JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
					total = Integer.parseInt(jsonobjectdata.get("total").toString());
					jsonlist = DataAnalyze.HospitalJsonToList(jsonarrayrows);
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
			        msg="数据解析出错";
			        code="-300";
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
		        msg="数据传输错误";
		        code="-200";
			}
		}
		
		map.put("msg", msg);
		map.put("rows", jsonlist);
		map.put("code", code);
        map.put("total", total);
		System.out.println("输出这个map："+map);
		return map;
	}
	/**
	 * 这里是医院端审核初始化界面，实现对所有医院端的符合条件的用户实现审核与否管理，（条件是：为未审核用户（即非正式用户），且未详细注册用户）
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/hospitalexamine")
	public ModelAndView hospitalexamine()throws Exception {
		//List<HosplogininfoCustom> hosplogininfoExamineList = hospitalService.InitHosplogininfoExmineList(null);
		ModelAndView model = new ModelAndView();
		//model.addObject("hosplogininfoExamineList", hosplogininfoExamineList);
		model.setViewName("View/HospitalMan/HospitalExamine");
		return model;
	}
	/**
	 * 类处理对医院审核界面的审核提交，实现了多id的循环提交即批量审核
	 * @param doctorloginids
	 * @param hosplogininfoCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/hospitalexamineSubmit")
	public @ResponseBody Map<String,Object> hospitalexamineSubmit(HttpServletRequest request) throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		String hosploginid = request.getParameter("hosploginid");
		String isagree = request.getParameter("isagree");
		boolean type = Boolean.parseBoolean(isagree);
		String idea = request.getParameter("idea");
		if(idea=="") {
			idea=null;
		}else {
			idea=idea.replace(" ", "");
		}
		String Param = "adminloginid="+adminloginid+"&hosploginid="+hosploginid+"&type="+type+"&idea="+idea;
		System.out.println("参数Param是："+Param);
		Map<String, Object> ExamineJson = new HashMap<>();
		String code="";
		String msg="";
		if(adminloginid!=null && hosploginid!=null && isagree!=null){
			JSONObject json = HttpRequest.sendPost("checkhospital",Param);
			System.out.println("服务器返回json信息："+json);
	        code = json.get("code").toString();
	        msg = json.get("msg").toString();
		}else{
			code="-100";
			msg="审核提交失败，检查提交数据";
		}
		ExamineJson.put("code", code);
		ExamineJson.put("msg", msg);
		return ExamineJson;
	}
	/**
	 * 根据ID返回医生详情
	 * @param id
	 * @param hospinfoCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="hospitaldetails")
	public ModelAndView hospitaldetails(@RequestParam(value = "id", required = true) Integer id,HospinfoCustom hospinfoCustom)throws Exception {
		Hosplogininfo hosplogininfodedatils = hospitalService.findHosplogininfoDatils(id);
		hospinfoCustom.setHosploginid(id);
		HospinfoCustom hospinfodetails = hospitalService.findHospinfoDetails(hospinfoCustom);
		System.out.println(hosplogininfodedatils+":"+hospinfodetails);
		ModelAndView model = new ModelAndView();
		model.addObject("hosplogininfodedatils", hosplogininfodedatils);
		model.addObject("hospinfodetails",hospinfodetails);
		model.setViewName("View/HospitalMan/HospitalDetails");
		return model;
	}
	/**
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getHospitalDetails")
	public @ResponseBody Map<String,Object>  getHosptialDetails(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid =(Integer)session.getAttribute("adminloginid");
		
		String hosploginid = request.getParameter("hosploginid");
		String hospitalparam = "adminloginid="+adminloginid+"&hosploginid="+hosploginid;
		Map<String,Object> jsontomap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		String code="";
		String msg="";
		if(adminloginid==null || hosploginid==null) {
			code="-100";
			msg="请先登录";
		}else {
			try {
				JSONObject HospitalDetailsJson = HttpRequest.sendPost("gethospitaldetail", hospitalparam);
				System.err.println("输出接收到的json数据是："+HospitalDetailsJson);
				
				msg = HospitalDetailsJson.get("msg").toString();
				code = HospitalDetailsJson.get("code").toString();
				try {
					jsontomap = DataAnalyzeDetails.HospitalJsonToDetails(HospitalDetailsJson.getJSONObject("data"));
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
					code="-300";
					msg="数据解析错误";
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
				code="-200";
				msg="数据请求错误";
			}
		}
		map.put("msg", msg);
		map.put("code", code);
		map.put("details", jsontomap);
		return map;
	}
	/**
	 * 医院信息修改提交
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/hospitalinfoSubmit")
	public @ResponseBody Map<String,Object> hospitalinfoSubmit(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid=(Integer)session.getAttribute("adminloginid");
		String hospname=request.getParameter("hospname");
		String hospregidcard=request.getParameter("hospregidcard");
		String hospphone=request.getParameter("hospphone");
		String hosplevelname=request.getParameter("hosplevelname");
		String hospabs=request.getParameter("hospabs");
		String hospfeature=request.getParameter("hospfeature");
		String hospadrprovince=request.getParameter("hospadrprovince");
		String hospadrcity=request.getParameter("hospadrcity");
		String hospadrarea=request.getParameter("hospadrarea");
		String hospadrother=request.getParameter("hospadrother");
		
		String param="adminloginid="+adminloginid+"&hospname="+hospname+"&hospregidcard="+hospregidcard+"&hospphone="+hospphone+
				"&hosplevelname="+hosplevelname+"&hospabs="+hospabs+"&hospfeature="+hospfeature+
				"&hospadrprovince="+hospadrprovince+"&hospadrcity="+hospadrcity+"&hospadrarea="+hospadrarea+"&hospadrother="+hospadrother;
		Map<String,Object> result = new HashMap<String,Object>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("edithospitalinfo", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
		
	}
}
